<?php

/**
 * Class CartpriceModel
 * 此模型用于计算商品的价格方面
 *
 * 商品的数据模型
 * goods_id
 * store_id
 * template_id
 *
 *
 */
class CartpriceModel extends BaseModel
{
    var $table  = 'cart';
    var $prikey = 'rec_id';
    var $_name  = 'cart';

    protected $goodsinfo = array();

    protected $addressinfo = array();

    public function setGoodsInfo($goodsinfo)
    {
        $this->goodsinfo = $goodsinfo;
    }

    /**
     * @param $addressinfo
     * ['country']
     * ['province']
     * ['city']
     * ['region']
     */
    public function setAddressInfo($addressinfo)
    {
        $this->addressinfo = $addressinfo;
    }

    /**
     * @param $region_id
     * 通过region_id来设置对应的地区信息
     *
     */
    public function setAddrInfoRegionId($region_id)
    {
        //找到当前的信息
        //找到父级信息
        //设置到当前的信息处理中
    }

    /**
     * 价格计算
     * @auth bzyy
     *
     */
    public function imputedPrice()
    {
        foreach($this->goodsinfo as $goods)
        {

        }
    }

    /**
     * 单品单地区固定配送方式的快递费用展示
     * 固定地区固定快递
     * 忽略此方式
     */
    public function goodsAddrExprePrice($goodsinfo,$express_id)
    {
        $addr = $this->addressinfo;
    }

    /**
     * 获取运费列表,可支持配送的
     * 固定到地区
     * 注意看此地区是否包邮
     *
     *
     */
    public function goodsAddrTemplist()
    {
//        $address = $this->addressinfo;
//        $address = array_filter($address);

        //地区条件
        //循环统计地区运费

        foreach($this->goodsinfo as $k => $goods)
        {

        }

    }

    /**
     * 根据某一个商品来匹配某一条规则
     * @auth bzyy
     * 2015-8-10 14:07:26
     *
     * 实现了，一个商品，选择一个默认的快递方式，来支付运费
     *
     */
    public function Template($goodsinfo){
        $address = $this->addressinfo;
        $address = array_filter($address);

        if(!isset($goodsinfo['post_template_id'])){
            $this->_error('未找到对应的运费模板');
        }
        $model = m('postaltemplate');
        $post_template_id = intval($goodsinfo['post_template_id']);
        $post_template_value = intval($goodsinfo['post_template_value']);
        //匹配地区
        $sql = "select * from ".$model->_prefix."post_express where postal_id='{$post_template_id}' order by is_default desc ";

        $pressinfo = $model->getRow($sql);
        if(empty($pressinfo)){
            return array('name'=>'默认包邮','exp_id'=>0,'rule_id'=>0,'post_fee'=>0,'is_free'=>1);
        }
        //echo '<pre>';print_r($pressinfo);
        //找到对应地区的价钱
        $where = "where rule.express_id='{$pressinfo['id']}' ";
        foreach($address as $k => $v)
        {
            $where .= " and {$k}='{$v}' ";
        }

        $sql = "select * from ".$model->_prefix."post_exp_rule as rule left join ".$model->_prefix."post_rule_region as region on rule.id = region.rule_id {$where} ";
        $info = $model->getRow($sql);
        if(empty($info)){
            $sql = "select * from ".$model->_prefix."post_exp_rule  where express_id='{$pressinfo['id']}' ";
            $info = $model->getRow($sql);
            if(empty($info)){
                $this->_error('未找到对应的数据');
                return false;
            }
        }

        //
        $postal_value = $post_template_value * $goodsinfo['number'];

        if($postal_value<=$info['first_value']){
            $info['postal_fee'] = $info['first_price'];
            return $info;
        }

        $overflow = $postal_value-$info['first_value'];
        $overmoney = ceil($overflow/$info['step'])*$info['step_price'];
        $info['postal_fee'] = $info['first_price'] + $overmoney;

        return $info;

    }


    /**
     * @param $goodsinfo
     *
     * 用于用户可以选择下拉框，用来进行展示
     */
    public function TemplateList($goodsinfo)
    {
        $address = $this->addressinfo;
        $address = array_filter($address);

        if(!isset($goodsinfo['post_template_id'])){
            $this->_error('未找到对应的运费模板');
        }
        $model = m('postaltemplate');
        $post_template_id = intval($goodsinfo['post_template_id']);
        $post_template_value = intval($goodsinfo['post_template_value']);
        //匹配地区
        $sql = "select * from ".$model->_prefix."post_express where postal_id='{$post_template_id}' order by is_default desc ";
        $express = $model->getAll($sql);

        if(empty($express)){
           return array(array('name'=>'默认包邮','exp_id'=>0,'rule_id'=>0,'post_fee'=>0,'is_free'=>1));
        }

        $express_names = array();
        foreach($express as $val)
        {
            $express_names[$val['id']] = $val['name'];
        }

        $postal_value = $post_template_value * $goodsinfo['number'];

        $express_info = array();

        $address_where = "";
        foreach($address as $k=>$v)
        {
            $address_where .= " and {$k}='{$v}' ";
        }

        foreach($express as $val)
        {
            $where = "where rule.express_id='{$val['id']}' {$address_where} ";

            $sql = "select rule.* from ".$model->_prefix."post_exp_rule as rule left join ".$model->_prefix."post_rule_region as region on rule.id = region.rule_id {$where} ";
            $info = $model->getRow($sql);

            if(empty($info)){
                $sql = "select * from ".$model->_prefix."post_exp_rule  where express_id='{$val['id']}' and ctype='2' ";
                $info = $model->getRow($sql);
                if(empty($info)){
                    continue;
                }
            }

            if(!isset($express_names[$info['express_id']]))
            {
                continue;
            }
            $info['express_name'] = $express_names[$info['express_id']];

            if($postal_value<=$info['first_value']){
                $info['postal_fee'] = $info['first_price'];
                $express_info[] = $info;
            }else{
                $overflow = $postal_value-$info['first_value'];
                if($info['step'] == 0){$info['step']=1;}
                $overmoney = ceil($overflow/$info['step'])*$info['step_price'];
                $info['postal_fee'] = $info['first_price'] + $overmoney;
                $express_info[] = $info;
            }

        }

        if(empty($express_info)){
            $this->_error('暂无数据可选');
            return false;
        }

        return $express_info;

    }

}

?>